package com.fenghongzhang.kuaishou.index.home.view.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghongzhang.kuaishou.R;
import com.fenghongzhang.kuaishou.index.home.MyLayoutManager;
import com.fenghongzhang.kuaishou.index.home.adapter.OptimalFragmentAdapter;
import com.fenghongzhang.kuaishou.index.home.entity.VideoBean;
import com.fenghongzhang.kuaishou.index.home.presenter.OptimalPresenter;
import com.fenghongzhang.kuaishou.utils.ActivityStatusBarUtil;
import com.fenghongzhang.kuaishou.widget.MyVideoPlayer;
import com.fenghongzhang.kuaishou.widget.TestVideoUrl;
import com.fenghongzhang.mvp_lib.view.BaseFragment;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 首页中的精选
 *
 * @author fenghongzhang
 * @date 2021-05-16
 */
public class OptimalFragment extends BaseFragment<OptimalPresenter> implements SurfaceHolder.Callback {

    private static final String TAG = "OptimalFragment";
    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.surface_view)
    SurfaceView surfaceView;
    MyLayoutManager myLayoutManager;
    OptimalFragmentAdapter optimalFragmentAdapter;
    //时间
    long time = 0;
    //坐标对象
    PointF pointF = new PointF();
    SurfaceHolder surfaceHolder;
    DisplayMetrics display;

    List<VideoBean> list;

    //清楚屏幕上的红心
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Canvas canvas = surfaceHolder.lockCanvas();
            Paint paint = new Paint();
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            paint.setColor(Color.TRANSPARENT);
            RectF rectF = new RectF();
            rectF.set(0, 0, display.widthPixels, display.heightPixels);
            canvas.drawRect(rectF, paint);
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    };

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        surfaceHolder = holder;
        surfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //计算点击的时间,判断是不是在指定时间内双击
                        if (time == 0) {
                            time = System.currentTimeMillis();
                        } else {
                            if (System.currentTimeMillis() - time < 200) {
                                pointF.x = event.getX();
                                pointF.y = event.getY();
                                drawHeart();
                                handler.sendEmptyMessageDelayed(1, 300);
                            } else {
                                time = System.currentTimeMillis();
                            }
                        }
                        break;
                }
                return false;
            }
        });
    }
    /**
     * 画一个红心
     */
    public void drawHeart() {
        Canvas canvas = surfaceHolder.lockCanvas();
        //把画笔设置为透明,清空所有覆盖的区域
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        paint.setColor(Color.TRANSPARENT);
        RectF rectF = new RectF();
        rectF.set(0, 0, display.widthPixels, display.heightPixels);
        canvas.drawRect(rectF, paint);
        //画一个小心心
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.detail_icon_like_doublepressed);
        canvas.drawBitmap(bitmap, pointF.x, pointF.y, null);
        surfaceHolder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        surfaceHolder = holder;
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_optimal;
    }

    @Override
    public void initView() {
        surfaceView = findViewById(R.id.surface_view);
        mRecyclerView = findViewById(R.id.recycler);
        surfaceView.setZOrderOnTop(true);
        surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
        surfaceView.getHolder().addCallback(this);
        display = getActivity().getResources().getDisplayMetrics();
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE:
                        MyVideoPlayer myVideoPlayer = myLayoutManager.getChildAt(0).findViewById(R.id.video_view);
                        myVideoPlayer.startPlayLogic();
                        break;
                }
            }
        });

    }
    @Override
    public void initData() {
         list = new ArrayList<>();
        for (int i = 0; i < TestVideoUrl.videoUrls.length; i++) {
            VideoBean videoBean = new VideoBean();
            list.add(videoBean);
        }
        myLayoutManager = new MyLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(myLayoutManager);
        //这里应该是更新适配器的内容
        optimalFragmentAdapter = new OptimalFragmentAdapter(getActivity(), list);

    }

    @Override
    public void showMsg(String msg) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        if(fragmentPresenter != null){
            fragmentPresenter.destroy();
            fragmentPresenter = null;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        //修改状态栏
        ActivityStatusBarUtil.setAndroidNativeLightStatusBar(getActivity(),false);
        Log.i(TAG, "loadData: optimal");
        GSYVideoManager.onResume();
        mRecyclerView.setAdapter(optimalFragmentAdapter);

    }

}
